package com.doyambean.springcloud.consul.clientdemo.controller;

import com.doyambean.springcloud.consul.clientdemo.service.ClientDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("clientdemo")
@RestController
public class ClientDemoController {

    @Autowired
    private ClientDemoService clientDemoService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/do")
    public String doClient() {
        String strLocal = clientDemoService.clientDemoSrv();

        String strRemote = getFar();
        return strLocal + "<-- from -->" + strRemote;
    }

    private String getFar() {
        String url = "http://server-demo/serverdemo/do";

        ParameterizedTypeReference<String> parameterizedTypeReference = new ParameterizedTypeReference<String>() {
        };
        ResponseEntity<String> r = restTemplate.exchange(url, HttpMethod.GET, null, parameterizedTypeReference);
        return r.getBody();
    }

}
